using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PCK
    {
        public clsCHI_TIET_PCK(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private CHI_TIET_PCK GetDataRow(DataRow row)
        {
            CHI_TIET_PCK objCHI_TIET_PCK = new CHI_TIET_PCK();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PCK.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKhoNguon"] != DBNull.Value)
                    objCHI_TIET_PCK.MaKhoNguon = (row["MaKhoNguon"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["TenKhoNguon"] != DBNull.Value)
                    objCHI_TIET_PCK.TenKhoNguon = (row["TenKhoNguon"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKhoNguonThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PCK.MaKhoNguonThamChieuSAP = (row["MaKhoNguonThamChieuSAP"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["MaKhoDich"] != DBNull.Value)
                    objCHI_TIET_PCK.MaKhoDich = (row["MaKhoDich"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["TenKhoDich"] != DBNull.Value)
                    objCHI_TIET_PCK.TenKhoDich = (row["TenKhoDich"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKhoDichThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PCK.MaKhoDichThamChieuSAP = (row["MaKhoDichThamChieuSAP"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuChuyenKho"] != DBNull.Value)
                    objCHI_TIET_PCK.MaPhieuChuyenKho = (row["MaPhieuChuyenKho"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["MaPhieuHuyChuyenKho"] != DBNull.Value)
                    objCHI_TIET_PCK.MaPhieuHuyChuyenKho = (row["MaPhieuHuyChuyenKho"].ToString().Trim());
            }
            catch { }


            try
            {
                if (row["TinhTrang"] != DBNull.Value)
                    objCHI_TIET_PCK.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            }
            catch { }

            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    objCHI_TIET_PCK.SoLuong = float.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    objCHI_TIET_PCK.MaThietBi = (row["MaThietBi"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objCHI_TIET_PCK.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PCK.MaTrangThai = int.Parse(row["MaTrangThai"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PCK.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["ValuationType"] != DBNull.Value)
                    objCHI_TIET_PCK.ValuationType = (row["ValuationType"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["NgayHuyChuyenKho"] != DBNull.Value)
                    objCHI_TIET_PCK.NgayHuyChuyenKho = Convert.ToDateTime(row["NgayHuyChuyenKho"].ToString().Trim());
            }
            catch { }
            try
            {
                if (row["MaNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PCK.MaNSX = (row["MaNhaSanXuat"].ToString().Trim());
            }
            catch { }

            return objCHI_TIET_PCK;
        }
        private List<CHI_TIET_PCK> GetCHI_TIET_PCK(DataTable dt)
        {
            List<CHI_TIET_PCK> lst = new List<CHI_TIET_PCK>();
            CHI_TIET_PCK obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        public List<CHI_TIET_PCK> usp_SelectCHI_TIET_PCK_ByMaPhieuChuyenKho(string MaPhieuChuyenKho)
        {
            string spName = "usp_SelectCHI_TIET_PCK_ByMaPhieuChuyenKho";
            DataTable dt = new DataTable();
            List<CHI_TIET_PCK> lst = new List<CHI_TIET_PCK>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaPhieuChuyenKho", MaPhieuChuyenKho);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PCK(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public int usp_UpdateCHI_TIET_PCK_Cancelation(List<CHI_TIET_PCK> lstCT)
        {
            string spName = "usp_UpdateCHI_TIET_PCK_Cancelation";
            int kq = 0;
            try
            {
                CHI_TIET_PCK obj;
                SqlHelper.BeginTran();
                int i = 0;
                for (i = 0; i < lstCT.Count; i++)
                {
                    obj = new CHI_TIET_PCK();
                    obj = lstCT[i];

                    SqlParameter[] par = new SqlParameter[10];
                    par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                    par[0].Direction = ParameterDirection.ReturnValue;
                    par[1] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                    par[2] = new SqlParameter("@SoLuong", obj.SoLuong);
                    par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                    par[4] = new SqlParameter("@MaPhieuChuyenKho", obj.MaPhieuChuyenKho);
                    par[5] = new SqlParameter("@MaPhieuHuyChuyenKho", obj.MaPhieuHuyChuyenKho);
                    par[6] = new SqlParameter("@MaKhoNguon", obj.MaKhoNguon);
                    par[7] = new SqlParameter("@MaKhoDich", obj.MaKhoDich);
                    par[8] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                    par[9] = new SqlParameter("@NgayTao", obj.NgayNhap);

                    if (!SqlHelper.IsHaveTran())
                        SqlHelper.ExecuteNonQuery(spName, par);
                    else
                    {
                        SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                    }
                    kq = int.Parse(par[0].Value.ToString());
                    if (kq <= 0)
                        SqlHelper.Tran.Rollback();
                }

                if (i == lstCT.Count)
                    SqlHelper.Tran.Commit();
                else
                    SqlHelper.Tran.Rollback();

            }
            catch (Exception ex)
            {
                kq = -1;
                //SqlHelper.Tran.Rollback();
            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_UpdateCHI_TIET_PCK(CHI_TIET_PCK obj)
        {
            string spName = "usp_UpdateCHI_TIET_PCK";
            int kq = 0;
            try
            {

                SqlHelper.OpenConnection();
                    SqlParameter[] par = new SqlParameter[10];
                    par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                    par[0].Direction = ParameterDirection.ReturnValue;
                    par[1] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                    par[2] = new SqlParameter("@SoLuong", obj.SoLuong);
                    par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                    par[4] = new SqlParameter("@MaPhieuChuyenKho", obj.MaPhieuChuyenKho);
                    par[5] = new SqlParameter("@MaPhieuHuyChuyenKho", obj.MaPhieuHuyChuyenKho);
                    par[6] = new SqlParameter("@MaKhoNguon", obj.MaKhoNguon);
                    par[7] = new SqlParameter("@MaKhoDich", obj.MaKhoDich);
                    par[8] = new SqlParameter("@NgayTao", obj.NgayNhap);
                    par[9] = new SqlParameter("@NguoiTao", obj.NguoiNhap);

                    if (!SqlHelper.IsHaveTran())
                        SqlHelper.ExecuteNonQuery(spName, par);
                    else
                    {
                        SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                    }
                    kq = int.Parse(par[0].Value.ToString());                 
                           

            }
            catch (Exception ex)
            {
                kq = -1;
               
            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_DeleteCHI_TIET_PCK(CHI_TIET_PCK obj)
        {
            string spName = "usp_DeleteCHI_TIET_PCK";
            int kq = 0;
            try
            {

                SqlHelper.OpenConnection();
                SqlParameter[] par = new SqlParameter[5];
                par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                par[0].Direction = ParameterDirection.ReturnValue;
                par[1] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);              
                par[2] = new SqlParameter("@MaPhieuChuyenKho", obj.MaPhieuChuyenKho);                
                par[3] = new SqlParameter("@NgayTao", obj.NgayNhap);
                par[4] = new SqlParameter("@NguoiTao", obj.NguoiNhap);

                if (!SqlHelper.IsHaveTran())
                    SqlHelper.ExecuteNonQuery(spName, par);
                else
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                }
                kq = int.Parse(par[0].Value.ToString());


            }
            catch (Exception ex)
            {
                kq = -1;

            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_InsertCHI_TIET_PCK(CHI_TIET_PCK obj)
        {
            string spName = "usp_InsertCHI_TIET_PCK";
            int kq = 0;
            try
            {

                SqlHelper.OpenConnection();
                SqlParameter[] par = new SqlParameter[11];

                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@SoLuong", obj.SoLuong);
                par[2] = new SqlParameter("@MaThietBi", obj.MaThietBi);
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@MaPhieuChuyenKho", obj.MaPhieuChuyenKho);
                par[5] = new SqlParameter("@MaPhieuHuyChuyenKho", obj.MaPhieuHuyChuyenKho);
                par[6] = new SqlParameter("@MaKhoNguon", obj.MaKhoNguon);
                par[7] = new SqlParameter("@MaKhoDich", obj.MaKhoDich);
                par[8] = new SqlParameter("@MaTrangThai", obj.MaTrangThai);
                par[9] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                par[10] = new SqlParameter("@NgayTao", obj.NgayNhap);
                

                if (!SqlHelper.IsHaveTran())
                    kq = SqlHelper.ExecuteNonQuery(spName, par);
                else
                {
                    kq = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                }              


            }
            catch (Exception ex)
            {
                kq = -1;

            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }
    }
}
